<?php
/*约单模块：首页button以及我的界面中的我的活动*/
class OrderAction extends Action{
//首页第四个button
	//获得约单活动栏目列表	
	public function showOrderColumn(){
		$column = M("Order_type")->select();
		for($i=0;$i<count($column);$i++){
			$image = D('attach')->where('attach_id='.$column[$i]['pic'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$column[$i]['pic'] = getImageUrl($image_url);
		}
		
		if ($column != false){
			$data=["code"=>"success","data"=>$column];
		}else{
			$data=["code"=>"wrong","data"=>M("Order_type")->getDbError()];
		}
		
		header("Content-Type: text/json");
		die(json_encode($data));
	}
	//活动约单活动列表 分页
	
	public function showOrder(){
		$tid = $_REQUEST["tid"];
		//分页设置
		$pagecount=isset($_REQUEST['pagecount'])?$_REQUEST['pagecount']:"10";//每页显示数量
		$pagenum=$pagecount*((isset($_REQUEST['pagenum'])?$_REQUEST['pagenum']:"1")-1);//当前页数
		
		$sql = "select * from yjorder where tid = $tid order by ctime desc limit $pagenum,$pagecount";
		$orders = M("Order")->query($sql);
		
		if ($orders === false){
			if(mysql_num_rows($orders)<1){
				$data=["code"=>"success","data"=>[]];
			}
			$data=["code"=>"wrong","data"=>M("Order_type")->getDbError()];
			
		}else{
			for($i=0;$i<count($orders);$i++){
				$data= M("User")->where("uid = ".$orders[$i]['uid'])->find();
				$orders[$i]['uid'] = $data["uname"];
				$orders[$i]['uphone'] = $data["phone"]; 
			}
			$data=["code"=>"success","data"=>$orders];
		}
		header("Content-Type: text/json");
		die(json_encode($data));
	}
	//参加约单活动:判断人数是否已经超过以及以及参加过，没有则添加并修改数据
	public function joinOrder(){
		$uid = $_REQUEST["uid"];//用户ID
		$oid = $_REQUEST["oid"];//约单ID
		//人数判断
		$data = M("Order")->where("oid = $oid")->find();
		if($data["people_limit"]<=$data["people_current"]){
			$res=["code"=>"over"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
		
		if($data["status"] != "going"){
			$res=["code"=>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
		//是否已经参加过
		$user = M("Order_data")->where("oid = ".$oid." and uid = ".$uid)->find();
		if(isset($user['id'])){
			//已经参加过了
			$res=["code"=>"already"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
		//用户参加约单的记录
		$order_data = M("Order_data");
		$order_data->uid = $uid;
		$order_data->oid = $oid;
		$order_data->ctime = time();
		$order_data->status = 0;
		if($order_data->add()){
			
			//添加成功
			//约单情况+1
			$res = M("Order")->where("oid = $oid")->setInc("people_current","oid = $oid");
			
			if($res){
				
				if($data["people_limit"] <= ($data["people_current"]+1)){
					$order = M("Order");
					$order->status = "full";
					$order->where("oid = $oid")->save();
				}
				$res=["code"=>"success"];
				header("Content-Type: text/json");
				die(json_encode($res));
			}
		}
		
		$res=["code"=>"wrong"];
		header("Content-Type: text/json");
		die(json_encode($res));
		
		
	}
//我的活动
	// 我的约单 ；3个状态未完成，完成，过期
	public function showMyOrder(){
		$uid = $_POST["uid"];
		$status = $_POST["status"];//状态：0 未完成 1 已完成 2 过期
		$sql = "select d.*,o.* from yjorder_data as d left join yjorder as o on d.oid = o.oid where d.uid = $uid and d.status = $status";
		$order = M("Order_data")->query($sql);
		if ($order == false){
			if(is_array($order)){
				$res=["code"=>"success","data"=>$order];
				header("Content-Type: text/json");
				die(json_encode($res));
			}
			$res=["code"=>"wrong","data"=>M("Order_data")->getDbError()];
			header("Content-Type: text/json");
			die(json_encode($res));
		}else{
			for($i=0;$i<count($order);$i++){
				$order[$i]["tid"] = M("Order_type")->where("tid = ".$order[$i]["tid"])->find()["o_type"];
			}
			
			$res=["code"=>"success","data"=>$order];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
	}
	//完成约单
	public function finishOrder(){
		$oid = $_POST["oid"];
		$uid = $_POST["uid"];
		$map['act_time'] = $_POST["act_time"];
		$map['cal'] = $_POST["cal"];
		$map['status'] = 1;
		//修改状态，添加运动数据
		if(M("Order_data")->where("oid = $oid and uid = $uid")->save($map)){
			//更新该约单的排名
			$this->updatePlace($oid);
			$res=["code"=>"success"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}else{
			$res=["code"=>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}		
	}
	//查看约单排名
	public function showOrderPlace(){
		$oid = $_POST["oid"];
		
		$sql ="select o.*,u.avatar,u.uname from yjorder_data as o left join yjuser as u on o.uid = u.uid where o.oid = $oid and status = 1 order by o.place desc";
		$data = M("Order_data")->query($sql);	
		if ($data != false){
			$result=["code"=>"success","data"=>$data];
			header("Content-Type: text/json");
			die(json_encode($result));
		}else{
			$result=["code"=>"wrong"];
			header("Content-Type: text/json");
			die(json_encode($result));
		}
	}
	//组织约单
	public function buildOrder(){
		//dump($_REQUEST);exit;
		$order = M("Order");
		$order->tid = t($_POST["tid"]);
		$order->uid = t($_POST["uid"]);
		$order->rule = t($_POST["rule"]);
		$order->ordername = t($_POST["ordername"]);
		$order->people_limit = t($_POST["people_limit"]);
		$order->end_time = t($_POST["end_time"]);
		$order->ctime = time();
		if ($order->add()){
			$oid = $order->getLastInsID();
			$order_data = M("Order_data");
			$order_data->uid = t($_POST["uid"]);
			$order_data->oid = $oid;
			$order_data->ctime = time();
			$order_data->status = 0;
			$order_data->add();
			$res=["code"=>"success"];
			header("Content-Type: text/json");
			die(json_encode($res));
		}else{
			$res=["code"=>"wrong","massage"=>$order->getDbError()];
			header("Content-Type: text/json");
			die(json_encode($res));
		}
		
	}	
	//检查过期
	public function checkOverOrder(){
		echo "check";
		
		$data = date("Ymd",time());
		$order = M("Order");
		$o_data = $order->where("status = 'going' or status = 'full'")->select();
		echo "order<br/>";
		for($j=0;$j<count($o_data);$j++){
			if(strtotime($o_data[$j]["end_time"])< time()){
				$sql="update yjorder set status = 'finish' where oid = ".$o_data[$j]["oid"];
				dump($o_data[$j]["oid"]."//".$order->query($sql));
			}
		}

		$data = date("Ymd",time());
		$order_data = M("Order_data");
// 		$order_data->status = 2;//改为过期
// 		$order_data->where("end_time < $data and status = 0")->save();	
		echo "order_data<br/>";
		$od_data = $order->where("status = 'finish' ")->select();
		for($k=0;$k<count($od_data);$k++){
			
			$sql = "update yjorder_data  set status = 1 where oid = ".$od_data[$k]['oid']." and (status = 0 or status = 2)";
			dump($od_data[$k]['oid']."//".$order_data->query($sql));
		}
		
		//教练课程
		$coach_order = M("Coach_order");
		$co_data = $coach_order->where("status = 0")->select();
		echo "coachorder<br/>";
		for($i=0;$i<count($co_data);$i++){
			echo time()."<br/>";
			echo strtotime($co_data[$i]["start_time"])."/".$co_data[$i]["start_time"]."//";
			echo strtotime($co_data[$i]["start_time"]) < time()."<br/>";
			if(strtotime($co_data[$i]["start_time"]) < time()){
				$sql="update yjcoach_order set status = 1 where oid = ".$co_data[$i]["oid"];
				dump($co_data[$i]["oid"]."/".$coach_order->query($sql));
			}
		}
	}
	
	//更新排名
	public function updatePlace($oid){
		$order = M("Order_data");
		$data = $order->where("oid = $oid and status = 1")->order("cal desc,act_time desc")->select();
		for($i=0;$i<count($data);$i++){
			$id = $data[$i]["id"];
			$p = $i+1;			
			$order->query("update yjorder_data set place = $p where id = $id");	
			
		}
	}
	
}